package com.sc.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.common.Bo.ItemParamBo;
import com.sc.common.pojo.CommonResult;
import com.sc.common.pojo.EasyUiDataGridResult;
import com.sc.common.utils.IDUtil;
import com.sc.mapper.TbItemDescMapper;
import com.sc.mapper.TbItemMapper;

import com.sc.mapper.TbItemParamItemMapper;
import com.sc.mapper.TbItemParamMapper;
import com.sc.pojo.*;
import com.sc.pojo.TbItemExample.Criteria;
import com.sc.service.ItemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * @author sc
 */
@Service
public class ItemServiceImpl implements ItemService {


    @Autowired
    private TbItemMapper itemMapper;

    @Autowired
    private TbItemDescMapper itemDescMapper;

    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;


    public TbItem getitemById(Long itemid) {
        TbItemExample example =  new TbItemExample();
        Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(itemid);
        List<TbItem> list = itemMapper.selectByExample(example);
        TbItem item = new TbItem();
        if(list!=null&&list.size()>0){
           item = list.get(0);
        }
        return item;
    }


    public EasyUiDataGridResult getItemList(int page, int rows) {
        PageHelper.startPage(page,rows);
        TbItemExample example =  new TbItemExample();
        List<TbItem> list = itemMapper.selectByExample(example);
        PageInfo<TbItem> pageInfo =  new PageInfo<TbItem>(list);
        EasyUiDataGridResult result = new EasyUiDataGridResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }



    /***
     * //商品状态 1.正常 2.下架 3.删除
     */
    public CommonResult createItem(TbItem item, String desc,String itemParams) {

        long itemId = IDUtil.genItemId();
        item.setId(itemId);
        item.setStatus((byte)1);
        itemMapper.insert(item);
        TbItemDesc tbItemDesc =  new TbItemDesc();
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setItemDesc(desc);
        itemDescMapper.insert(tbItemDesc);
        if(itemParams!=null&&itemParams.length()>0){
            TbItemParamItem tbItemParamItem = new TbItemParamItem();
            tbItemParamItem.setParamData(itemParams);
            tbItemParamItem.setItemId(item.getId());
            tbItemParamItemMapper.insert(tbItemParamItem);
        }
        return CommonResult.ok();
    }


}
